package com.kb.crawl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import com.car.util.DocUtil;
import com.car.util.Util;
import com.kb.bean.KBOfBitAuto;
import com.kb.dao.KBDaoOfBitAuto;

public class KBCrawlOfBitAuto implements Runnable {
	static Logger logger = Logger.getLogger(KBCrawlOfBitAuto.class.getName());
	private KBDaoOfBitAuto kbDaoOfBitAuto;
	private String urlSpell;

	public KBCrawlOfBitAuto(KBDaoOfBitAuto kbDaoOfBitAuto, String urlSpell) {
		super();
		this.kbDaoOfBitAuto = kbDaoOfBitAuto;
		this.urlSpell = urlSpell;
	}

	@Override
	public void run() {
		startCrawl(this.urlSpell);
	}

	// 得到页数 page
	public int getPages(String urlSpell) {
		int pages = 1;
		try {
			String url = "http://car.bitauto.com/" + urlSpell + "/koubei/tags/";
			Document doc = DocUtil.getDoc(url);
			Elements page = doc.select("div.the_pages a");
			pages = Integer.parseInt(page.get(page.size() - 2).text());
		} catch (Exception e) {
		}
		return pages;
	}

	public void startCrawl(String urlSpell) {
		int page = getPages(urlSpell);
		logger.info(urlSpell + " 开始！");
		int j = 0;
		for (int i = 1; i < page + 1; i++) {
			String url = "http://car.bitauto.com/" + urlSpell + "/koubei/tags/%E7%BB%BC%E5%90%88/page" + i + "/";
			Document doc = DocUtil.getDoc(url);
			Elements aLink = doc.select("a.tittxt");
			for (Element a : aLink) {
				String link = a.attr("href");
				if (link != null && link.length() > 0) {
					KBContent(link);
					j++;
				}
			}

		}
		logger.info(urlSpell + " 结束！ 共" + j + "条口碑");
		kbDaoOfBitAuto.updateBBSTime("bitauto_kbgxsj", urlSpell);
	}

	// 具体口碑的内容
	public void KBContent(String url) {
		Document doc = DocUtil.getDoc(url);
		try {
			int kb_kbid = Integer.parseInt(url.split("koubei/")[1].split("/")[0]);
			String kb_urlspell = url.split("/")[3];
			Element carInfo = doc.select("div.kb_car_messbox div.tit1 a").get(0);
			String kb_cxid = carInfo.attr("href").split("/")[7].split("m")[1];
			String kb_cxmc = carInfo.text();
			String kb_bt = doc.select("h1.con").text();
			String userInfo = doc.select("span.author a").get(0).attr("href");
			if (userInfo == null || userInfo.length() == 0) {
				userInfo = "0000";
			}
			int kb_yhid = Integer.parseInt(Util.getMatched(userInfo, "\\d{4,}").group());
			String kb_plrq = doc.select("p.article-information span.time").text();
			double kb_syyh = 0;
			double kb_wg = 0;
			double kb_ns = 0;
			double kb_kj = 0;
			double kb_dl = 0;
			double kb_ck = 0;
			double kb_pz = 0;
			double kb_xjb = 0;
			double kb_ssx = 0;
			double kb_yh = 0;
			Elements comments = doc.select("div.kb_car_messbox ul:not(.top) li");
			for (Element comment : comments) {
				String label = comment.select("span.label").text();
				String fen = comment.select("span.fen").text();
				switch (label) {
				case "外 观：":
					kb_wg = Double.parseDouble(fen.substring(0, 1).equals("-") ? "00" : fen.substring(0, 1));
					break;
				case "内 饰：":
					kb_ns = Double.parseDouble(fen.substring(0, 1).equals("-") ? "00" : fen.substring(0, 1));
					break;
				case "空 间：":
					kb_kj = Double.parseDouble(fen.substring(0, 1).equals("-") ? "00" : fen.substring(0, 1));
					break;
				case "动 力：":
					kb_dl = Double.parseDouble(fen.substring(0, 1).equals("-") ? "00" : fen.substring(0, 1));
					break;
				case "操 控：":
					kb_ck = Double.parseDouble(fen.substring(0, 1).equals("-") ? "00" : fen.substring(0, 1));
					break;
				case "配 置：":
					kb_pz = Double.parseDouble(fen.substring(0, 1).equals("-") ? "00" : fen.substring(0, 1));
					break;
				case "性 价 比：":
					kb_xjb = Double.parseDouble(fen.substring(0, 1).equals("-") ? "00" : fen.substring(0, 1));
					break;
				case "舒 适 度：":
					kb_ssx = Double.parseDouble(fen.substring(0, 1).equals("-") ? "00" : fen.substring(0, 1));
					break;
				case "油 耗：":
					kb_yh = Double.parseDouble(fen.substring(0, 1).equals("-") ? "00" : fen.substring(0, 1));
					break;

				default:
					break;
				}
			}
			String kb_plnr = "";
			Elements nr = doc.select("div.article-contents p");
			for (Element e : nr) {
				for (TextNode text : e.textNodes()) {
					if (text.text().trim().length() > 0) {
						kb_plnr = kb_plnr + "\n" + (text.text().trim());
					}
				}
			}
			kb_plnr = Util.deleteChar(kb_plnr);
			int kb_plcd = kb_plnr.length();
			String kb_gmdd = "";
			String kb_jxs = "";
			String kb_gmsj = "";
			double kb_gmjg = 0;
			Elements purchase = doc.select("ul.top li");
			for (Element info : purchase) {
				String key = info.select("span.label").text();
				String value = info.select("span:not(.label)").text();
				switch (key) {
				case "裸 车 价：":
					value = value.equals("暂无") ? "0" : Util.getMatched(value, "\\d+\\.\\d+").group();
					kb_gmjg = Double.parseDouble(value);
					break;
				case "购车城市：":
					kb_gmdd = value;
					break;
				case "经 销 商：":
					kb_jxs = value;
					break;
				case "上牌时间：":
					kb_gmsj = value;
					break;

				default:
					break;
				}
			}

			int kb_lc = 0;
			int kb_dzs = 0;
			int kb_djl = 0;
			int kb_hfs = Integer.parseInt(doc.select("a#a_PostNum").text());
			int kb_zhpf = Integer.parseInt(doc.select("div.big_start_box strong").text().substring(0, 1));
			double kb_myfy = 0;
			String kb_zqsj = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			int kb_sfjh = doc.select("span.jinhua_kb").size() > 0 ? 1 : 0;
			String kb_yhdj = "";
			int kb_tzs = 0;
			int kb_tzjh = 0;
			String kb_dq = "";
			String kb_zc = "";
			KBOfBitAuto kbOfBitAuto = new KBOfBitAuto(kb_kbid, kbDaoOfBitAuto.urlSpellToID(kb_urlspell), kb_cxid,
					kb_cxmc, kb_bt, kb_yhid, kb_plrq, kb_gmdd, kb_jxs, kb_kj, kb_dl, kb_ck, kb_syyh, kb_yh, kb_ssx,
					kb_wg, kb_ns, kb_xjb, kb_pz, kb_plnr, kb_plcd, kb_gmsj, kb_gmjg, kb_lc, kb_dzs, kb_djl, kb_hfs,
					kb_zhpf, kb_myfy, kb_zqsj, kb_sfjh, kb_yhdj, kb_tzs, kb_tzjh, kb_dq, kb_zc);
			kbDaoOfBitAuto.insertKB(kbOfBitAuto);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(url);
		}
	}

}
